<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <h2>直接通过FormData</h2>
    <!-- e.target.files[0] -->
    <!-- 推荐用户选择 图片 -->
    <input type="file" accept="image/*" />
    <img src="" alt="" />
    <!-- 点击按钮 上传用户选择的图片 -->
    <button>上传</button>
    <script src="./lib/axios.js"></script>

    <script>
      // 1.点击按钮上传图片
      // document.querySelector('button').addEventListener('click',()=>{
      //   const img=document.querySelector('input').files[0];
      //   const data=new FormData()

        
      // })
      axios.defaults.baseURL='http://ajax-api.itheima.net'

      document.querySelector('button').onclick=()=>{
        const img =document.querySelector('input').files[0]
        const data=new FormData()
        data.append('avatar',img)
        axios({
          method:'post',
          url:'/api/file',
          data:data,
        }).then((res)=>{
          // console.log(res);
          document.querySelector('img').src=res.data.data.url
        })
      }
    </script>
  </body>
</html>
